package com.common.utils
{
	import com.common.formatters.NumberFormatter;
	import com.common.formatters.NumberBaseRoundType;
	
	public class NumberUtil
	{
		public function NumberUtil()
		{
		}
		/**
		 *格式化數據 
		 * @param num 數據大小
		 * @param precision 小數點后位數
		 * @return 
		 */		
		public static function formatNumber(num:Number,precision:int = 0):String
		{
			var numFormat :NumberFormatter = new NumberFormatter();
			numFormat.precision = precision;
			numFormat.useThousandsSeparator = true;
			numFormat.rounding = NumberBaseRoundType.NEAREST;
			
			return numFormat.format(num);
		}
		/**
		 * 把有格式的字符串转化为Number
		 * @param value
		 * @param thousandsSeparator
		 * @return 
		 * 
		 */		
		public static function formatStringToNumber(value:String,thousandsSeparator:String = ",",precision:int = 2):Number
		{
			if(value == null)
			{
				return 0;
			}
			var myPattern:RegExp = /,/g;
			
			var destStr :String = value.replace(myPattern,"");
			var destNum :Number = Number(destStr);
			
			var numFormat :NumberFormatter = new NumberFormatter();
			numFormat.precision = precision;
			numFormat.rounding = NumberBaseRoundType.NEAREST;
			
			var formatSource :String = numFormat.format(destNum);
			
			var formatStr :String = formatSource.replace(myPattern,"");
			
			return Number(formatStr);
		}
		/**
		 *是否是奇数 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function isOdd(value:int):Boolean
		{
			if(value%2 == 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}